package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.PaasListGroupDao;
import com.xbongbong.paas.domain.entity.PaasListGroupEntity;
import com.xbongbong.paas.domain.entity.ext.PaasListGroupEntityExt;
import com.xbongbong.paas.model.PaasListGroupModel;
import com.xbongbong.paas.toolbox.util.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wufeng
 * @date: 2018/8/13 15:22
 * @desrcption: PaaS流程设计实现类
 */
@Service("paasListGroupModel")
public class PaasListGroupModelImpl implements PaasListGroupModel {

    /**
     * 不需要显示条件的检索
     */
    private static final String NO_HAS_CONDITION_COLUMNS = " id, corpid, app_id, form_id, name, sort, creator_id, add_time, update_time, del ,default_group_code";

    private static final String COLUMNS = "*,IF(default_group_code > 0,1,0) as default_group";


    @Resource
    private PaasListGroupDao paasListGroupDao;

    @Override
    public Integer insert(PaasListGroupEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return paasListGroupDao.insert(entity);
    }

    @Override
    public List<PaasListGroupEntity> list(Map<String, Object> map) {
//        map.put("columns", new StringBuffer(NO_HAS_CONDITION_COLUMNS));
        return paasListGroupDao.findEntitys(map);
    }

    @Override
    public List<PaasListGroupEntity> listByUserId(String corpid, Long appId, Long formId, String userId, String orderBy) {
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("appId", appId);
        params.put("formId", formId);
        params.put("creatorId", userId);
        params.put("del", 0);
        params.put("orderByStrGroup", orderBy);
        return list(params);
    }

    @Override
    public PaasListGroupEntity getByKey(Long key, String corpid) {

        return paasListGroupDao.getByKey(key, corpid);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return paasListGroupDao.getEntitysCount(map);
    }

    @Override
    public Integer delete(Long key, String corpid) {
        return  paasListGroupDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer update(PaasListGroupEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return paasListGroupDao.update(entity);
    }

    @Override
    public void sort(List<PaasListGroupEntity> list, String corpid) {
        paasListGroupDao.sort(list, corpid);
    }

    @Override
    public void insertBatch(List<PaasListGroupEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        paasListGroupDao.insertBatch(list);
    }

    @Override
    public List<PaasListGroupEntityExt> listExt(Map<String, Object> params) {
        params.put("columns",COLUMNS);
        return paasListGroupDao.findEntityExts(params);
    }
    
    /**
     * 删除编号为52的红冲退款分组
     *
     * @return
     */
    @Override
    public Integer deleteByDefaultGroupCode() {
        return paasListGroupDao.deleteByDefaultGroupCode();
    }
}
